﻿using System;
using Landpy.CruiseControlNet.ConfigurationSidekick.Resources;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    class ProjectHandler : UpdateChildrenNode
    {
        public override void UpdateChildrenNodes(ChildrenNodeContext childrenNodeContext)
        {
            var treeNodeIndentity = this.GetTreeNodeIndentity(childrenNodeContext);
            var treeNodesBuilder = this.GetTreeNodesBuilder(childrenNodeContext.CruiseControl);
            if (treeNodeIndentity.Type == TreeNodeType.Project)
            {
                var queryProject = this.ProjectRepository.GetProjectById(childrenNodeContext.CruiseControl, new Guid(treeNodeIndentity.Key));
                if (childrenNodeContext.ChangedValueKey == typeof(Trigger).Name)
                {
                    var projectTriggersTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Triggers];
                    if (projectTriggersTreeNode != null)
                    {
                        projectTriggersTreeNode.Nodes.Clear();
                        if (queryProject.Triggers != null)
                        {
                            foreach (var trigger in queryProject.Triggers)
                            {
                                treeNodesBuilder.BuildTriggerNode(trigger, projectTriggersTreeNode, queryProject.Id);
                            }
                        }
                    }
                    else
                    {
                        projectTriggersTreeNode = treeNodesBuilder.BuildTriggersNode(queryProject, childrenNodeContext.SelectedNode);
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    if (projectTriggersTreeNode != null)
                    {
                        projectTriggersTreeNode.Expand();
                    }
                }
                if (childrenNodeContext.ChangedValueKey == typeof(Task).Name)
                {
                    var projectTasksTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Tasks];
                    if (projectTasksTreeNode != null)
                    {
                        projectTasksTreeNode.Nodes.Clear();
                        if (queryProject.Triggers != null)
                        {
                            foreach (var task in queryProject.Tasks)
                            {
                                treeNodesBuilder.BuildTaskNode(task, projectTasksTreeNode, queryProject.Id);
                            }
                        }
                    }
                    else
                    {
                        projectTasksTreeNode = treeNodesBuilder.BuildTasksNode(queryProject, childrenNodeContext.SelectedNode);
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    projectTasksTreeNode.Expand();
                }
                if (childrenNodeContext.ChangedValueKey == typeof(Publisher).Name)
                {
                    var projectPublishersTreeNode = childrenNodeContext.SelectedNode.Nodes[Resource.TreeNode_Publishers];
                    if (projectPublishersTreeNode != null)
                    {
                        projectPublishersTreeNode.Nodes.Clear();
                        if (queryProject.Publishers != null)
                        {
                            foreach (var publisher in queryProject.Publishers)
                            {
                                switch (publisher.Type)
                                {
                                    case PublisherType.XmlLogger:
                                        treeNodesBuilder.BuildXmlLoggerNode(publisher, projectPublishersTreeNode, queryProject.Id);
                                        break;
                                    case PublisherType.Email:
                                        treeNodesBuilder.BuildEmailNode(publisher, projectPublishersTreeNode, queryProject.Id);
                                        break;
                                    case PublisherType.ArtifactCleanup:
                                        treeNodesBuilder.BuildArtifactCleanupNode(publisher, projectPublishersTreeNode, queryProject.Id);
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }
                    }
                    else
                    {
                        projectPublishersTreeNode = treeNodesBuilder.BuildPublishersNode(queryProject, childrenNodeContext.SelectedNode);
                    }
                    childrenNodeContext.SelectedNode.Expand();
                    if (projectPublishersTreeNode != null)
                    {
                        projectPublishersTreeNode.Expand();
                    }
                }
            }
            else
            {
                this.Successor.UpdateChildrenNodes(childrenNodeContext);
            }
        }
    }
}
